package com.admin.scroller.photoorvideo.utils;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.Build;

import java.util.ArrayList;

/**
 * Created by 吴昶 on 2018/9/13.
 */
public class PermissionUtil {
    public static final int GET_PERMISSION=0x1001;

    public static final int GET_PERMISSION_STORAGE=0x1003;

    public static final int GET_PERMISSION_CAMERA=0x1004;
    /**
     *
     * @param permissions
     * @return  false 不需要申请权限   true需要申请权限
     */
    public static boolean getPermission(String[] permissions, Activity activity,int requestCode){
        if(permissions==null||permissions.length==0) return false;
        int pid = Binder.getCallingPid();
        int uid = Binder.getCallingUid();
        ArrayList<String> per=new ArrayList<String>();
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M) {
            for (String permission : permissions) {
                if (activity.checkPermission(permission, pid, uid) != PackageManager.PERMISSION_GRANTED) {
                    per.add(permission);
                }
            }
            if (per.size() > 0) {
                activity.requestPermissions(per.toArray(new String[per.size()]), requestCode);
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }
}

